/**************************************************************/
/*  This file is licensed from, and is a trade secret of:     */
/*                                                            */
/*    Mercury Computer Systems, Incorporated                  */
/*    199 Riverneck Road                                      */
/*    Chelmsford, Massachusetts 01824-2820                    */
/*    United States of America                                */
/*    Telephone + 1 978 256-1300                              */
/*                                                            */
/*  Refer to your Software License Agreement for provisions   */
/*  on use, duplication, or disclosure.                       */
/*  US. Government users and prime contractors are also       */
/*  governed by the following Data Rights Notice:             */
/*  Use, duplication or disclosure by the US. Government is   */
/*  subject to the provisions set forth in FAR 52.227-14 for  */
/*  restricted rights or other agency data rights provisions  */
/*  on commercial software and documentation, as applicable.  */
/*                                                            */
/*  This file: (a) was developed at private expense, and no   */
/*  part was developed with government funds, (b) is a        */
/*  trade secret of Mercury Computer Systems, Inc. for the    */
/*  purposes of the Freedom of Information Act, (c) is        */
/*  "commercial computer software" subject to limited         */
/*  utilization as provided in the above noted Software       */
/*  License Agreement and (d) is in all respects data         */
/*  belonging exclusively to Mercury Computer Systems, Inc..  */
/*                                                            */
/**************************************************************/

/******************************************************************/
/* Copyright (c) 1999-2003, Mercury Computer Systems, Inc.,       */
/* Chelmsford MA.,and all third party embedded software sources.  */
/* All rights reserved under the Copyright laws of US. and        */
/* international treaties.                                        */
/*                                                                */
/* Patent Pending                                                 */
/******************************************************************/


//$COPYRIGHT_END


#include <SceOsSemaphore.h>
#include <SceOsSizeCheck.h>
#include <cassert>
#include <climits>
#include <string>
#include <windows.h>
#include "SceOsWin32Error.h"

inline
HANDLE &
o2h (char * ptr)
  throw ()
{
  return *reinterpret_cast<HANDLE *> (ptr);
}

SCE::OS::Semaphore::Semaphore (unsigned int initial)
  throw (std::string)
{
  assert ((compileTimeSizeCheck<sizeof (m_osOpaque), sizeof (HANDLE)> ()));
  assert (sizeof (m_osOpaque) >= sizeof (HANDLE));

  if (((o2h (m_osOpaque)) = CreateSemaphore (0, initial, LONG_MAX, 0)) == 0) {
    std::string reason = "error creating semaphore: ";
    throw reason;
  }
}

SCE::OS::Semaphore::~Semaphore ()
  throw ()
{
  CloseHandle (o2h (m_osOpaque));
}

void
SCE::OS::Semaphore::post ()
  throw (std::string)
{
  ReleaseSemaphore (o2h (m_osOpaque), 1, 0);
}

bool
SCE::OS::Semaphore::wait (int timeout_msecs)
  throw (std::string)
{
  if (0 == timeout_msecs) {
    timeout_msecs = INFINITE;
  }
  return(WAIT_OBJECT_0 == WaitForSingleObject (o2h (m_osOpaque), timeout_msecs));
}

